package com.server.net.session;

import com.msg.MsgMain;
import com.server.net.message.MsgDispatcherManager;
import io.netty.channel.Channel;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 链接管理器
 */
public class SessionManager {

    private static SessionManager instance = new SessionManager();

    public static SessionManager getInstance() {
        return instance;
    }

    //channelId<>Session
    private final Map<String, Session> sessionMap = new ConcurrentHashMap<>();

    //token<>channelId
    private final Map<String, String> tokenMap = new ConcurrentHashMap<>();

    //userId<>channelId
    private final Map<String, String> userMap = new ConcurrentHashMap<>();

    //roleId<>channelId
    private final Map<Long, String> roleMap = new ConcurrentHashMap<>();

    private SessionManager() {

    }

    public Session getSessionByToken(String token) {
        String channelId = tokenMap.get(token);
        if (null == channelId) {
            return null;
        }
        return getSessionByChannelId(channelId);
    }

    public Session getSessionByChannelId(String channelId) {
        if (null == channelId || channelId.isEmpty()) {
            return null;
        }
        return sessionMap.get(channelId);
    }

    public Session getSessionByUserId(long userId) {
        String channelId = userMap.get(userId);
        return getSessionByChannelId(channelId);
    }

    public Session getSessionByRoleId(long roleId) {
        String channelId = roleMap.get(roleId);
        return getSessionByChannelId(channelId);
    }

    /**
     * 建立链接
     */
    public void connect(Channel channel) {
        Session session = new Session(channel);
        sessionMap.put(session.getId(), session);
    }

    /**
     * 链接断开
     */
    private void disconnect(Session session) {
        roleMap.remove(session.getRoleId());
        userMap.remove(session.getUserId());
        tokenMap.remove(session.getToken());
        sessionMap.remove(session.getId());
        MsgDispatcherManager.getInstance().offLine(session);
    }

    public void disconnect(Channel channel) {
        String channelId = channel.id().asLongText();
        Session session = getSessionByChannelId(channelId);
        disconnect(session);
    }

    /**
     * 账号登录，获取或创建userId
     */
    public boolean userLogin(String token, String channelId) {
        String oldChannelId = tokenMap.get(token);
        if (oldChannelId == null) {
            tokenMap.put(token, channelId);
            return true;
        }
        if (oldChannelId.equals(channelId)) {
            //重复登录
            return false;
        } else {
            //断线重连，换链接了
            tokenMap.put(token, channelId);
        }
        return true;
    }

    /**
     * 发送信息
     */
    public void sendMsgByToken(String token, MsgMain.MainProtocl.Builder mainProtocl) {
        String channelId = tokenMap.get(token);
        if (null == channelId) {
            return;
        }
        Session session = sessionMap.get(channelId);
        if (null == session) {
            return;
        }
        session.sendMsg(mainProtocl);
    }

    public void sendMsgByRoleId(long roleId, MsgMain.MainProtocl.Builder mainProtocl) {
        String channelId = roleMap.get(roleId);
        if (null == channelId) {
            return;
        }
        Session session = sessionMap.get(channelId);
        if (null == session) {
            return;
        }
        session.sendMsg(mainProtocl);
    }

    public void sendMsgByUserId(long userId, MsgMain.MainProtocl.Builder mainProtocl) {
        String channelId = userMap.get(userId);
        if (null == channelId) {
            return;
        }
        Session session = sessionMap.get(channelId);
        if (null == session) {
            return;
        }
        session.sendMsg(mainProtocl);
    }
}
